package com.rongke.damo.medicalrecord.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import jodd.util.StringUtil;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.rongke.biz.service.MedicalRecordManager;
import com.rongke.biz.service.UserManager;
import com.rongke.commons.JsonResp;
import com.rongke.enums.CancerTypeEnum;
import com.rongke.model.InspectionRecord;
import com.rongke.model.MedicalRecords;
import com.rongke.model.Radiotherapy;
import com.rongke.model.ReportRecord;
import com.rongke.model.User;
import com.rongke.model.dict.DictInspection;
import com.rongke.service.DictInspectionService;
import com.rongke.service.InspectionRecordService;
import com.rongke.service.MedicalRecordsService;
import com.rongke.service.ReportRecordService;
import com.rongke.service.UserService;
import com.rongke.utils.Md5;
import com.rongke.utils.UserIdGenerator;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * 医生诊断书
 * 
 * @author xiaosen.jxs
 * @version $Id: DamoReportRecordController.java, v 0.1 2017年10月11日 下午6:56:42 xiaosen.jxs Exp $
 */
@RestController
@RequestMapping(value = "/api/damo/reportRecord")
@Transactional
public class DamoReportRecordController {
    private static final Logger  LOGGER = Logger.getLogger(DamoReportRecordController.class);

    @Autowired
    private ReportRecordService  reportRecordService;

    @Autowired
    private UserManager          userManager;

    @Autowired
    private MedicalRecordManager medicalRecordManager;

    /**
     * @添加检查记录
     * @param inspectionRecord
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public JsonResp addReportRecord(@RequestBody Map<String, String> jsonMap) {
        LOGGER.debug("add reportRecord in damo");

        try {
            String mobileNo = jsonMap.get("phone");
            if (StringUtil.isEmpty(mobileNo)) {
                return JsonResp.fa(null);
            }

            User user = userManager.selectOrAddUser(mobileNo);
            if (null == user) {
                return JsonResp.fa(null);
            }
            String userId = user.getUserId();

            MedicalRecords mrInfo = medicalRecordManager.selectOrAddMrByUserId(userId);
            if (null == mrInfo) {
                return JsonResp.fa(null);
            }
            long mrId = mrInfo.getId();

            //创建一个新的检查记录
            ReportRecord rrInfo = new ReportRecord();
            rrInfo.setMedicalRecordId(mrId);
            rrInfo.setUserId(userId);
            rrInfo.setTnmInfo(jsonMap.get("tnmInfo"));
            rrInfo.setTreatmentDate(jsonMap.get("treatmentDate"));
            rrInfo.setPicContent(jsonMap.get("picContent"));
            rrInfo.setTagInfo(jsonMap.get("tagInfo"));

            reportRecordService.insert(rrInfo);
            JSONObject json = new JSONObject();
            json.put("id", rrInfo.getId());
            return JsonResp.ok(json);
        } catch (Exception e) {
            LOGGER.error("addReportRecord failed", e);
            return JsonResp.fa(null);
        }
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public JsonResp updateReportRecord(@RequestBody ReportRecord reportRecord) {
        LOGGER.debug("updateReportRecord");

        try {
            ReportRecord rrInfo = new ReportRecord();
            rrInfo.setId(reportRecord.getId());
            rrInfo.setTnmInfo(reportRecord.getTnmInfo());
            rrInfo.setTreatmentDate(reportRecord.getTreatmentDate());
            rrInfo.setPicContent(reportRecord.getPicContent());
            rrInfo.setTagInfo(reportRecord.getTagInfo());

            reportRecordService.updateById(rrInfo);

            JSONObject json = new JSONObject();
            json.put("id", reportRecord.getId());
            return JsonResp.ok(json);
        } catch (Exception e) {
            LOGGER.error("updateInspectionRecord failed", e);
            return JsonResp.fa(null);

        }
    }

    /**
     * 根据ID删除检查记录
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public JsonResp delReportRecordById(long id) {
        if (LOGGER.isInfoEnabled()) {
            LOGGER.info("delReportRecordById, id=" + id);
        }

        try {
            reportRecordService.deleteById(id);

            JSONObject json = new JSONObject();
            json.put("id", id);
            return JsonResp.ok(json);
        } catch (Exception e) {
            LOGGER.error("delReportRecordById failed, id=" + id, e);
            return JsonResp.fa(null);
        }
    }

    /**
     * @根据id查找病历记录报告
     * @param id
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/selectOne", method = RequestMethod.GET)
    public JsonResp selectReportRecord(long id) {
        LOGGER.debug("selectReportRecord");

        try {
            ReportRecord reportRecord = reportRecordService.selectById(id);
            return JsonResp.ok(reportRecord);
        } catch (Exception e) {
            LOGGER.error("selectReportRecord failed, id=" + id, e);
            return JsonResp.fa(null);
        }
    }

    /**
     * 根据具体的病历，获取所有的医生诊断书
     * 
     * @param medicalRecordId
     * @return
     */
    @RequestMapping(value = "/selectAllByPhone", method = RequestMethod.GET)
    public JsonResp selectList(String userId) {
        if (LOGGER.isInfoEnabled()) {
            LOGGER.info("selectList, userId=" + userId);
        }

        try {
            MedicalRecords mr = medicalRecordManager.selectByUserId(userId);
            if (null == mr) {
                return JsonResp.ok();
            }

            long mrId = mr.getId();

            EntityWrapper<ReportRecord> ew = new EntityWrapper<ReportRecord>();
            ew.eq("medical_record_id", mrId);
            ew.orderBy("treatment_date");
            List<ReportRecord> result = reportRecordService.selectList(ew);
            return JsonResp.ok(result);
        } catch (Exception e) {
            LOGGER.error("selectList failed, userId=" + userId, e);
            return JsonResp.fa(null);
        }
    }
}
